<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
</head>
<body>
	<button type="button" class="layui-btn" id="test1">
	  <i class="layui-icon">&#xe67c;</i>上传头像
	</button>
    <div id="headImgDiv">
    </div>

	<script src="../../js/constant.js"></script>
	<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="../../js/jq.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript">
	initData();
	
	function initData(){
		$.ajax({
			type : 'get',
			url : domainName + '/api-u/users/current',
			async : false,
			success : function(data) {
				var url = data.headImgUrl;
				if(url != null && url != ""){
                    var a = "<a target='_blank' href='" + url +"'><img src='" + url + "'></img></a>";
					$("#headImgDiv").html(a);
				}
			}
		});
	}
	
	layui.use(['upload','layer'], function(){
	  var layer = layui.layer;
	  var upload = layui.upload;
	  upload.render({
	    elem: '#test1' //绑定元素
	    ,accept: 'images' //允许上传的文件类型
	    ,url: domainName + '/zuul/api-f/files' //上传接口
	    ,done: function(res, index, upload){
	    	var url = res.url;
	    	var formdata = {headImgUrl:url};
			  $.ajax({
					type : 'put',
					url : domainName + '/api-u/users/me',
					contentType: "application/json; charset=utf-8",  
					data : JSON.stringify(formdata),
					success : function(data) {
						var a = "<a target='_blank' href='" + url +"'><img src='" + url + "'></img></a>";
						$("#headImgDiv").html(a);
						
						var img = parent.$(".admin-header-user img");
		     			img.attr("src", url);
						
						layer.msg("成功", {shift: -1, time: 1000}, function(){
							refresh_token(); //刷新当前登录用户缓存
		                });
					}
				});
	    }
	  });
	});
	
	</script>
</body>
</html>